package com.ruoyi.store.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.store.domain.StockTransfer;
import com.ruoyi.store.domain.StockTransferDetail;
import com.ruoyi.store.mapper.StockTransferDetailMapper;
import com.ruoyi.store.mapper.StockTransferMapper;
import com.ruoyi.store.service.IStockTransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 调拨管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-05
 */
@Service
@Slf4j
@Transactional
public class StockTransferServiceImpl implements IStockTransferService {
    @Autowired
    private StockTransferMapper stockTransferMapper;

    @Autowired
    private StockTransferDetailMapper stockTransferDetailMapper;

    /**
     * 查询调拨管理
     *
     * @param transferCoded
     * @return 调拨管理
     */
    @Override
    public StockTransfer selectStockTransferById(String transferCoded) {

        log.info("StockTransferServiceImpl  selectStockTransferById transferCode: " + transferCoded);
        StockTransfer stockTransfer = stockTransferMapper.getTransferBycode(transferCoded);
        log.info("StockTransferServiceImpl  selectStockTransferById stockTransfer:{} ", transferCoded);
        List<StockTransferDetail> list = stockTransferDetailMapper.getDetailByCode(transferCoded);
        stockTransfer.setDetailList(list);
        return stockTransfer;
    }

    @Override
    public List<StockTransfer> getAllTransfer() {
        return stockTransferMapper.getAllTransfer();
    }

    /**
     * 查询调拨管理列表
     *
     * @param stockTransfer 调拨管理
     * @return 调拨管理
     */
    @Override
    public List<StockTransfer> selectStockTransferList(StockTransfer stockTransfer) {
        return stockTransferMapper.selectStockTransferList(stockTransfer);
    }

    /**
     * 新增调拨管理
     *
     * @param stockTransfer 调拨管理
     * @return 结果
     */
    @Override
    public String insertStockTransfer(StockTransfer stockTransfer) {
        log.info("StockTransferServiceImpl insertStockTransfer start...stockTransfer:{}", stockTransfer);
        String code = "DB" + System.currentTimeMillis();
        stockTransfer.setTransferCode(code);
        stockTransfer.setCreateBy(SecurityUtils.getUsername());
        int count = stockTransferMapper.insertStockTransfer(stockTransfer);
        log.info("StockTransferServiceImpl insertStockTransfer end...count" + count);
        if (count < 0) {
            log.error("StockTransferServiceImpl insertStockTransfer error...");
            throw new ServiceException("系统错误，调拨订单添加失败");
        }
        List<StockTransferDetail> detailList = stockTransfer.getDetailList();
        for (StockTransferDetail detail : detailList) {
            detail.setTransferCode(code);
            detail.setCreateBy(SecurityUtils.getUsername());
            int count1 = stockTransferDetailMapper.addTransferDetail(detail);
            if (count1 < 0) {
                log.error("StockTransferServiceImpl insertStockTransfer error...");
                throw new ServiceException("系统错误，调拨订单添加失败");
            }
        }
        return "添加成功";
    }

    /**
     * 修改调拨管理
     *
     * @param stockTransfer 调拨管理
     * @return 结果
     */
    @Override
    public int updateStockTransfer(StockTransfer stockTransfer) {
        return stockTransferMapper.updateStockTransfer(stockTransfer);
    }

    /**
     * 批量删除调拨管理
     *
     * @param ids 需要删除的调拨管理主键
     * @return 结果
     */
    @Override
    public int deleteStockTransferByIds(Integer[] ids) {
        return stockTransferMapper.deleteStockTransferByIds(ids);
    }

    /**
     * 删除调拨管理信息
     *
     * @param id 调拨管理主键
     * @return 结果
     */
    @Override
    public int deleteStockTransferById(Integer id) {
        return stockTransferMapper.deleteStockTransferById(id);
    }
}
